﻿using System.Collections.Generic;
using System.Configuration;

namespace OpenWaves.EPiServer.BrightcoveVideoCloudProperty
{
    public class VideoCloudPropertySettings : ConfigurationSection
    {
        [ConfigurationProperty("access", IsRequired = true)]
        public AccessElement Access
        {
            get
            {
                return (AccessElement) this["access"];
            }
        }

        [ConfigurationProperty("players", IsRequired = true)]
        public PlayersCollection Players
        {
            get
            {
                return (PlayersCollection)this["players"];
            }
        }

        public IEnumerable<PlayerItem> PlayersList
        {
            get
            {
                for (var i = 0; i < this.Players.Count; i++)
                {
                    yield return new PlayerItem
                        {
                            Name = this.Players[i].Name,
                            PlayerKey = this.Players[i].PlayerKey,
                            PlayerId = this.Players[i].PlayerId,
                        };
                }
            }
        }
    }

    public class PlayerItem
    {
        public string Name { get; set; }
        public string PlayerKey { get; set; }
        public string PlayerId { get; set; }
    }

    public class AccessElement : ConfigurationElement
    {
        [ConfigurationProperty("readToken", IsRequired = true)]
        public string ReadToken
        {
            get
            {
                return (string)this["readToken"];
            }
        }
    }

    public class PlayersCollection : ConfigurationElementCollection
    {
        public Player this[int index]
        {
            get
            {
                return base.BaseGet(index) as Player;
            }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }


        protected override ConfigurationElement CreateNewElement()
        {
            return new Player();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((Player)element).Name;
        }
    }

    public class Player : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get
            {
                return this["name"] as string;
            }
        }

        [ConfigurationProperty("playerId", IsRequired = true)]
        public string PlayerId
        {
            get
            {
                return this["playerId"] as string;
            }
        }

        [ConfigurationProperty("playerKey", IsRequired = true)]
        public string PlayerKey
        {
            get
            {
                return this["playerKey"] as string;
            }
        }
    }
}